//
//  XYTitlePageView.swift
//  XMGTV
//
//  Created by Gary on 2018/3/8.
//  Copyright © 2018年 coderwhy. All rights reserved.
//

import UIKit

protocol XYTitlePageViewDelegate:class {
    
    func titlePageView(titlePage: XYTitlePageView , targetIndex : Int)
    
}

class XYTitlePageView: UIView {
    
    weak var delegate : XYTitlePageViewDelegate?
    
    fileprivate var titles : [String]
    fileprivate var attribute : XYPageAttribute
    fileprivate lazy var titleData = [UILabel]()
    fileprivate var preIndex : NSInteger = 0
    
    fileprivate lazy var scrollerView :UIScrollView = {
        let frame = CGRect(x: 0.0, y: 0.0, width: self.bounds.width, height: self.bounds.height)
        let scrolerV = UIScrollView(frame: frame)
        scrolerV.showsHorizontalScrollIndicator = false
        return scrolerV
    }()
    
    
    init(frame: CGRect ,titles : [String] , attribute : XYPageAttribute) {
        
        self.titles = titles
        self.attribute = attribute

        super.init(frame: frame)

        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XYTitlePageView{
    
    fileprivate func setupUI(){
        setupScrolerView()
        setupTitltLab()
    }
    
    private func setupScrolerView(){
        addSubview(scrollerView)
    }
    
    private func setupTitltLab() {
        var x : CGFloat = 0.0
        let y : CGFloat = 0
        var w : CGFloat = 0.0
        let h : CGFloat = attribute.titleHeight

        
        for (i,title) in titles.enumerated() {
            
            if attribute.isEnableScroller {
                //能够滑动
                w = (title as NSString).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: 0.0), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: attribute.fontSize)], context: nil).width
                
                if i==0{
                    x = attribute.HorizontalSpace*0.5
                }else{
                    let perLabe = titleData[i-1];
                    x = perLabe.frame.maxX + attribute.HorizontalSpace
                }
                
            }else{
                w = bounds.width/CGFloat(titles.count)
                x = CGFloat(i)*w;
            }
            
            let label :UILabel = UILabel(frame: CGRect(x: x, y: y, width: w, height: h))
            label.text = title
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: attribute.fontSize)
            label.textColor = i==0 ? attribute.selectedColor : attribute.normalColor
            scrollerView.addSubview(label)
            
            label.isUserInteractionEnabled = true
            label.tag = i + 555
            label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(titleLabTap(targetLabe:))))
            titleData.append(label)
            
        }
        
        let labe = titleData.last
        scrollerView.contentSize = CGSize(width: attribute.isEnableScroller ? labe!.frame.maxX + attribute.HorizontalSpace*0.5 : labe!.frame.maxX, height: 0.0)
        
    }
    
}

extension XYTitlePageView{
    @objc fileprivate func titleLabTap(targetLabe: UIGestureRecognizer){
        
        //上一个 label
        let preLab = titleData[preIndex]
        //点击的 label
        let targetLab = targetLabe.view as! UILabel
        
        preLab.textColor = attribute.normalColor
        targetLab.textColor = attribute.selectedColor
        
        
        preIndex = targetLabe.view!.tag - 555
        
        if attribute.isEnableScroller {
            //设置 偏移量
            var offsetX = targetLab.center.x - scrollerView.bounds.width*0.5
            
            if offsetX < 0{
                offsetX = 0
            }
            
            if offsetX > scrollerView.contentSize.width - scrollerView.bounds.width{
                offsetX = scrollerView.contentSize.width - scrollerView.bounds.width
            }
            
            scrollerView.setContentOffset(CGPoint(x: offsetX, y: 0.0), animated: true)
            
        }
        
        delegate?.titlePageView(titlePage: self, targetIndex: preIndex)
        
    }
    
}





